package controller.statistical;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import domain.entity.charge.Camera;
import domain.entity.rate.RateInfo;
import domain.service.charge.CameraService;
import domain.service.charge.ChargeService;
import domain.service.charge.ChargeServices;
import domain.service.rate.RateService;
import org.bouncycastle.cert.ocsp.Req;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import util.HttpRequestUtil;
import util.HttpRequestUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/6.
 */
@Controller
public class StatisticalController {

    private static final Logger logger = LoggerFactory.getLogger(StatisticalController.class);

    @Autowired
    private ChargeService chargeService;

    @Autowired
    private RateService rateService;

    @Autowired
    private CameraService cameraService;

    @Autowired
    private ChargeServices chargeServices;


    /**
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="getMolecular",method = RequestMethod.GET)
    public Map<String,Object> getMolecular(HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        return chargeServices.getMolecular();
    }



    /**
     * 获取投资
     * @param year
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="getMonthMoney",method = RequestMethod.GET)
    public Map<String,Object> getMonthMoney(String year,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        return chargeServices.getMonthMoney(year);
    }


    /**
     * 获取进度统计
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="GetRateInfo1",method = RequestMethod.GET)
    public List<Double> togetMonthMoney(HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        List<Double> returnData = new ArrayList<Double>();
        String jsonResult = HttpRequestUtils.httpGetReturnString("http://125.72.16.102:8081/XN/rest/RoadService2/GetRate1");
        if(jsonResult!=null){
            JSONArray array = JSONArray.parseArray(jsonResult);
            for( int i=0;i<array.size();i++ ){
                JSONObject obj = array.getJSONObject(i);
                if(obj.containsKey("rate")){
                    String rate = obj.getString("rate");
                    rate = rate.substring(0,rate.lastIndexOf("%"));
                    returnData.add(Double.parseDouble(rate));
                }
            }
        }
        return returnData;
    }


    @ResponseBody
    @RequestMapping(value="GetRateInfo2",method = RequestMethod.GET)
    public Map<String,Object> GetRateInfo2(String bd,HttpServletResponse response) throws UnsupportedEncodingException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        bd = new String(bd.getBytes("ISO-8859-1"), "UTF-8");
        Map<String,Object> resultMap = new HashMap<String, Object>();
        List<String> nameArray = new ArrayList<String>();
        List<Double> rateArray = new ArrayList<Double>();
        String jsonResult = HttpRequestUtils.httpGetReturnString("http://125.72.16.102:8081/XN/rest/RoadService2/GetRate2?bd="+bd);
        if(jsonResult!=null){
            JSONArray array = JSONArray.parseArray(jsonResult);
            for( int i=0;i<array.size();i++ ){
                JSONObject obj = array.getJSONObject(i);
                if(obj.containsKey("item")){
                    nameArray.add(obj.getString("item"));
                    String rate = obj.getString("rate");
                    rate = rate.substring(0,rate.lastIndexOf("%"));
                    rateArray.add(Double.parseDouble(rate));
                }
            }
        }
        resultMap.put("nameArray",nameArray);
        resultMap.put("rateArray",rateArray);
        return resultMap;
    }

     @ResponseBody
    @RequestMapping(value="GetRateInfo3",method = RequestMethod.GET)
    public Map<String,Object> GetRateInfo3(String bd, String prj,HttpServletResponse response) throws UnsupportedEncodingException {
         response.setHeader("Access-Control-Allow-Origin", "*");
         bd = new String(bd.getBytes("ISO-8859-1"), "UTF-8");
         prj = new String(prj.getBytes("ISO-8859-1"), "UTF-8");
         Map<String,Object> resultMap = new HashMap<String, Object>();
         List<String> nameArray = new ArrayList<String>();
         List<Double> rateArray = new ArrayList<Double>();
         String jsonResult = HttpRequestUtils.httpGetReturnString("http://125.72.16.102:8081/XN/rest/RoadService2/GetRate3?bd="+bd+"&prj="+prj);
         if(jsonResult!=null){
             JSONArray array = JSONArray.parseArray(jsonResult);
             for( int i=0;i<array.size();i++ ){
                 JSONObject obj = array.getJSONObject(i);
                 if(obj.containsKey("item")){
                     nameArray.add(obj.getString("item"));
                     String rate = obj.getString("rate");
                     rate = rate.substring(0,rate.lastIndexOf("%"));
                     rateArray.add(Double.parseDouble(rate));
                 }
             }
         }
         resultMap.put("nameArray",nameArray);
         resultMap.put("rateArray",rateArray);
         return resultMap;
    }

    @ResponseBody
    @RequestMapping(value="GetRateInfo4",method = RequestMethod.GET)
    public Map<String,Object> GetRateInfo4(String bd, String prj,String c1,HttpServletResponse response) throws UnsupportedEncodingException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        bd = new String(bd.getBytes("ISO-8859-1"), "UTF-8");
        prj = new String(prj.getBytes("ISO-8859-1"), "UTF-8");
        c1 = new String(c1.getBytes("ISO-8859-1"), "UTF-8");
        Map<String,Object> resultMap = new HashMap<String, Object>();
        List<String> nameArray = new ArrayList<String>();
        List<Double> rateArray = new ArrayList<Double>();
        String jsonResult = HttpRequestUtils.httpGetReturnString("http://125.72.16.102:8081/XN/rest/RoadService2/GetRate4?bd="+bd+"&prj="+prj+"&c1="+c1);
        if(jsonResult!=null){
            JSONArray array = JSONArray.parseArray(jsonResult);
            for( int i=0;i<array.size();i++ ){
                JSONObject obj = array.getJSONObject(i);
                if(obj.containsKey("item")){
                    nameArray.add(obj.getString("item"));
                    String rate = obj.getString("rate");
                    rate = rate.substring(0,rate.lastIndexOf("%"));
                    rateArray.add(Double.parseDouble(rate));
                }
            }
        }
        resultMap.put("nameArray",nameArray);
        resultMap.put("rateArray",rateArray);
        return resultMap;
    }

    @ResponseBody
    @RequestMapping(value="GetRateInfo5",method = RequestMethod.GET)
    public Map<String,Object> GetRateInfo5(String bd, String prj,String c1,String c2,HttpServletResponse response) throws UnsupportedEncodingException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        bd = new String(bd.getBytes("ISO-8859-1"), "UTF-8");
        prj = new String(prj.getBytes("ISO-8859-1"), "UTF-8");
        c1 = new String(c1.getBytes("ISO-8859-1"), "UTF-8");
        c2 = new String(c2.getBytes("ISO-8859-1"), "UTF-8");
        Map<String,Object> resultMap = new HashMap<String, Object>();
        List<String> cateArr = new ArrayList<String>();
        List<Double> data1 = new ArrayList<Double>();
        List<Double> data2 = new ArrayList<Double>();
        String jsonResult = HttpRequestUtils.httpGetReturnString("http://125.72.16.102:8081/XN/rest/RoadService2/GetRate5?bd="+bd+"&prj="+prj+"&c1="+c1+"&c2="+c2);
        if(jsonResult!=null){
            JSONArray array = JSONArray.parseArray(jsonResult);
            for( int i=0;i<array.size();i++ ){
                JSONObject obj = array.getJSONObject(i);
                if(obj.containsKey("item")){
                    cateArr.add(obj.getString("item"));
                    String finishqty = obj.getString("finishqty");
                    data1.add(Double.parseDouble(finishqty));

                    String designqty = obj.getString("designqty");
                    data2.add(Double.parseDouble(designqty));
                }
            }
        }
        resultMap.put("cateArr",cateArr);
        resultMap.put("data1",data1);
        resultMap.put("data2",data2);
        return resultMap;
    }

    /**
     * 获取视频列表
     * @return
     */
    @RequestMapping("getCameraList")
    @ResponseBody
    public List<Map<String,Object>> getCameraList(HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        List<Map<String,Object>> list = cameraService.getCameraList();
        for(Map<String,Object> objMap : list){
            Map<String,Object> dataMap = new HashMap<String, Object>();
            dataMap.put("value",objMap.get("ID"));
            dataMap.put("text",objMap.get("NAME"));
            dataMap.put("url",objMap.get("URL"));
            dataMap.put("position",objMap.get("POSITION"));
            resultList.add(dataMap);
        }
        return resultList;
    }

    @RequestMapping("getById")
    @ResponseBody
    public Map<String,Object> getById(String id,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", "*");
        return cameraService.getById(id);
    }

}
